//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		screenHeight:app.screenHeight,
		account:app.setting.account,
		pagetitle:'分类',
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		var route=page.route;
		page.setData({
			route:route,
		});
		if(opt.isgoshare){
				page.setData({
					ShowGoshare:true,
				});
		}
		if(route){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
		}
		if(opt){
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access,function(pagedata){
			//console.log('groups');console.log(pagedata);
			pagedata.shopset.adimg = pagedata.shopset.adimg?app.setting.att_url + pagedata.shopset.adimg:app.setting.siteroot+'/core/resource/images/nopic.jpg'
			pagedata.params=pagedata.params?pagedata.params:{};
			pagedata.list=pagedata.list?pagedata.list:[];
			page.setData({
				pluginnav:app.session.menus,
				pagedata:pagedata,
				action:app.access.plugin+'/'+app.access.action+'/',
				ShowAuthM:pagedata.show_setmember==2?true:false,
				nodatapic_close:true,
			});
			setTimeout(()=>{
				page.getgoods();
			},100);
		});
	},
	//翻页
	onReachBottom: function () {
		var isnodata=this.data.isnodata
		var loading=this.data.loading
		if(!isnodata && !loading){
			this.get_list()
		}
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	getnowchilds:function(e){
		var the=this;
		var data=e.currentTarget.dataset;
		var index=data.index;
		var pagedata=the.data.pagedata
		if(pagedata.islevel3){
			pagedata.params.scate=pagedata.nowchilds[index].id
		}else{
			pagedata.params.ccate=pagedata.nowchilds[index].id
		}
		pagedata.nowchildid = pagedata.nowchilds[index].id
		the.setData({
			'pagedata.params':pagedata.params,
			'pagedata.nowchildid':pagedata.nowchildid,
		});
		the.get_list(true);
	},
	getgoods:function(e){
		var the=this;
		var data=e?e.currentTarget.dataset:{};
		var index=data.index?data.index:0;
		var pagedata=the.data.pagedata
		if(index=='-1'){
			pagedata.pid=0
			pagedata.childs=pagedata.recommend;
			pagedata.image=''
			pagedata.advurl=''
		}else{
			pagedata.nowckey=index
			pagedata.category[index]=pagedata.category[index]?pagedata.category[index]:{'title':'全部'}
			pagedata.nowchilds=pagedata.category[index].childs
			pagedata.image=pagedata.category[index].advimg
			pagedata.advurl=pagedata.category[index].advurl
			pagedata.pid = pagedata.category[index].id;
			if(pagedata.islevel3){
				pagedata.params.ccate=pagedata.category[index].id?pagedata.category[index].id:0
			}else{
				pagedata.params.pcate=pagedata.category[index].id
			}
			pagedata.nowchilds=pagedata.nowchilds?pagedata.nowchilds:{}
			if(pagedata.nowchilds.length>0){
				pagedata.nowchildid=pagedata.nowchilds[0].id
				if(pagedata.islevel3){
					pagedata.params.scate=pagedata.nowchilds[0].id
				}else{
					pagedata.params.ccate=pagedata.nowchilds[0].id
				}
			}else{
				pagedata.nowchildid=0
				if(pagedata.islevel3){
					pagedata.params.scate=0
				}else{
					pagedata.params.ccate=0
				}
			}
		}
		the.setData({
				'pagedata.nowckey':pagedata.nowckey,
				'pagedata.nowchildid':pagedata.nowchildid,
				'pagedata.nowchilds':pagedata.nowchilds,
				'pagedata.image':pagedata.image,
				'pagedata.advurl':pagedata.advurl,
				'pagedata.params':pagedata.params,
				'pagedata.pid':pagedata.pid,
		});
		the.get_list(true);
	},
	selectkeyword:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		if(e){
			if(e.currentTarget){
				var data=e.currentTarget.dataset;
				if(data.type=='price'){
					pagedata.params.order='price';
					pagedata.params.order_type=pagedata.params.order_type==1?2:1;
				}else if(data.type=='sales'){
					pagedata.params.order='sales';
				}else if(data.type=='new'){
					pagedata.params.order='new';
				}else if(data.type=='canle'){
					pagedata.params={};
				}else{
					pagedata.params.order=data.type;
				}
				if(data.juli){
					pagedata.params.juli=data.juli;
				}
				if(data.keyword){
					pagedata.params.keyword=data.keyword;
				}
			}
			pagedata.params.page = 1;
			pagedata.list=[];
			isnodata=false;
			the.setData({
					'pagedata.list':pagedata.list,
					'pagedata.params':pagedata.params,
					isnodata:isnodata,
					'pagedata.childs':[]
			});
			the.get_list(true);
		}
	},
	get_list:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		if(e){
			pagedata.params.page = 1;
			pagedata.list=[];
			isnodata=false;
		}else{
			if(isnodata){
				return false;
			}
		}
		pagedata.params.is_specs=1;
		var query=app.util.made_query(pagedata.params);
		//console.log('query');console.log(query);
		app.util.get('/goods.index/getlist/'+query,function(res){
			console.log('goods');console.log(res);
			if(res=='none'){
				the.setData({
					isnodata:true,
					'pagedata.list':pagedata.list,
					'pagedata.params':pagedata.params,
				});
			}else{
				pagedata.list = pagedata.list.concat(res)
				pagedata.params.page++
				the.setData({
					'pagedata.list':pagedata.list,
					'pagedata.params':pagedata.params,
					isnodata:isnodata,
					loading:false,
				});
			}
			
		});
	},
	selectone:function(e){
		var the = this
		var data=e.currentTarget.dataset;
		var index=data.k;
		var pagedata=the.data.pagedata;
		pagedata.oneid = pagedata.onegroups[index].id;
		pagedata.category=pagedata.onegroups[index].childs;
		pagedata.params.pcate=pagedata.onegroups[index].id;
		the.setData({
			'pagedata.oneid':pagedata.oneid,
			'pagedata.params':pagedata.params,
			'pagedata.category':pagedata.category,
		});
		the.getgoods(0);
	},
	// 搜索框文本内容的显示
	keyword: function (e) {
		var the = this
		the.setData({
		  'pagedata.params.keyword': e.detail.value
		})
	},
	paramscid:function(e){
		var page=this
		var data=e.currentTarget.dataset;
		var pagedata=page.data.pagedata;
		if(data.cid && pagedata.pid==0 && data.parentid==0){
			 wx.navigateTo({'url':'/jy_weishop/goods/index/index?pid='+data.cid});
		}else{
			 wx.navigateTo({'url':'/jy_weishop/goods/index/index?cid='+data.cid});
		}
	},
	shownewpicker:function(e){
		var the=this;
		var pagedata=the.data.pagedata;
		var data=e.currentTarget.dataset;
		pagedata.g=pagedata.list[data.index];
		pagedata.nowgkey=data.index
		var shownewpicker=the.data.shownewpicker?false:true
		the.setData({
			'pagedata.g':pagedata.g,
			'pagedata.nowgkey':pagedata.nowgkey,
			shownewpicker:shownewpicker
		})
	},
	//减少
	minus:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var data=e.currentTarget.dataset;
		var index=data.index?data.index:0
		var g = pagedata.list[index]
		if(g.carttotal<=0){
			return false;
		}
		app.util.showLoading('减少中...');
		app.util.post('jy_weishop/member.cart/minus/',{'getlist':true,'goodsid':g.id},function(res){
				app.util.hideLoading();
				if(the.data.pluginnav && the.data.pluginnav.length>0){	
						for(var i=0;i<the.data.pluginnav.length;i++){
							var action=the.data.pluginnav[i].action
							if(action.indexOf("jy_weishop/member.cart") != -1){
								the.data.pluginnav[i].nums=the.data.pluginnav[i].nums?the.data.pluginnav[i].nums:0
								the.data.pluginnav[i].nums--
							}
						}
				}
				app.session.menus=the.data.pluginnav;
				g.carttotal--;
				pagedata.list[index]=g
				the.setData({
					'pagedata.list':pagedata.list,
					'pluginnav':the.data.pluginnav
				})
		});
	},
	//增加
    plus:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var data=e.currentTarget.dataset;
		var index=data.index?data.index:0
		var g = pagedata.list[index]
		g.optionid=0;
		if(g.carttotal>=g.total){
			app.util.message('不能再添加了，已超过库存！','error');
			return false;
		}
		app.util.showLoading('加入中...');
		app.util.post('jy_weishop/member.cart/addtocart/',{'getlist':true,'goodsid':g.id,'optionid':g.optionid},function(res){
				app.util.hideLoading();
				if(the.data.pluginnav && the.data.pluginnav.length>0){	
						for(var i=0;i<the.data.pluginnav.length;i++){
							var action=the.data.pluginnav[i].action
							if(action.indexOf("jy_weishop/member.cart") != -1){
								the.data.pluginnav[i].nums=the.data.pluginnav[i].nums?the.data.pluginnav[i].nums:0
								the.data.pluginnav[i].nums++
							}
						}
				}
				app.session.menus=the.data.pluginnav;
				g.carttotal++;
				pagedata.list[index]=g
				the.setData({
					'pagedata.list':pagedata.list,
					'pluginnav':the.data.pluginnav
				})
		});
	},
	sp_minus:function(){
		var the=this;
		var pagedata=the.data.pagedata
		var g =pagedata.g;
		if(g.carttotal<=0){
			return false;
		}
		app.util.showLoading('减少中...');
		app.util.post('jy_weishop/member.cart/minus/',{'getlist':true,'goodsid':g.id,'optionid':g.options.id},function(res){
				app.util.hideLoading();
				if(res=='success'){
					return false;
				}
				if(g.post[g.options.id].num<=0){
					return false;
				}
				if(the.data.pluginnav && the.data.pluginnav.length>0){	
						for(var i=0;i<the.data.pluginnav.length;i++){
							var action=the.data.pluginnav[i].action
							if(action.indexOf("jy_weishop/member.cart") != -1){
								the.data.pluginnav[i].nums=the.data.pluginnav[i].nums?the.data.pluginnav[i].nums:0
								the.data.pluginnav[i].nums--
							}
						}
				}
				app.session.menus=the.data.pluginnav;
				g.carttotal--;
				pagedata.carts=res.list
				g.post[g.options.id].num--;
				pagedata.list[pagedata.nowgkey]=g
				the.setData({
					'pagedata.g':g,
					'pagedata.list':pagedata.list,
					'pluginnav':the.data.pluginnav
				})
		});
	},
	//增加
	sp_plus:function(e){
		var the=this
		var pagedata=the.data.pagedata
		var g =pagedata.g;
		if(parseInt(g.carttotal)>=parseInt(g.options.stock)){
			app.util.message('不能再添加了，已超过库存！','error');
			return false;
		}
		app.util.showLoading('加入中...');
		app.util.post('jy_weishop/member.cart/addtocart/',{'getlist':true,'goodsid':g.id,'optionid':g.options.id},function(res){				
				app.util.hideLoading();
				if(res!='success'){
					if(res.cartid>0){
						g.cartid=res.cartid;
					}
					if(the.data.pluginnav && the.data.pluginnav.length>0){	
						for(var i=0;i<the.data.pluginnav.length;i++){
							var action=the.data.pluginnav[i].action
							if(action.indexOf("jy_weishop/member.cart") != -1){
								the.data.pluginnav[i].nums=the.data.pluginnav[i].nums?the.data.pluginnav[i].nums:0
								the.data.pluginnav[i].nums++
							}
						}
					}
					app.session.menus=the.data.pluginnav;
					g.carttotal++;
					g.post[g.options.id].num++;
					pagedata.list[pagedata.nowgkey]=g
					console.log(pagedata.nowckey)
					the.setData({
						'pagedata.g':g,
						'pagedata.list':pagedata.list,
						'pluginnav':the.data.pluginnav
					})
				}
		});
	},
	get_optionsid:function(e){
		var the=this
		var pagedata=the.data.pagedata
		var specid = pagedata.g.specid;
		var data=e.currentTarget.dataset;
		pagedata.g.specid[data.index]=data.specid
		specid = specid.join('_');
		console.log(specid);
		app.util.post('jy_weishop/goods.detail/get_optionsid/',{'goodsid':pagedata.g.id,'specid':specid},function(res){
				var optionsid=res.options.id;
				//console.log(res);
				pagedata.g.options=res.options;
				pagedata.g.selespename=res.selespename;
				if(!pagedata.g.post[optionsid]){
					pagedata.g.post[optionsid]={}
					pagedata.g.post[optionsid]={
						'num':pagedata.g.post[optionsid].num?pagedata.g.post[optionsid].num:res.carttotal,
						'title':res.selespename,
					}
				}
				the.setData({
					'pagedata.g':pagedata.g
				})
		});
	},
	gotoitem:function(e){
		var pagedata=this.data.pagedata
		app.util.get('jy_weishop/goods.detail//id='+pagedata.goods.id);
	},
	onChooseAvatar:function(e){
		//获取头像
		var the=this;
		var avatarUrl= e.detail.avatarUrl
		the.setData({
			avatarUrl:avatarUrl
		})
		app.avatarUrl=avatarUrl
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		var the=this;
		app.util.getUserInfo(e,function(res){
			if(res && res.userInfo){
				app.isgetUserInfo=true
				the.setData({
					ShowAuthM:false
				})
				the.onLoad();
			}else{
				return false;
			}
		});
	},
	mobile_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户手机号
		var the = this
		var pagedata= the.data.pagedata
		app.util.post('core/member.login/wxapp_login/return_mobile=1',e.detail,function(res){
			the.setData({
				ShowAuthM:false
			})
		});
	},
	closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('post',data);
	},
	onShareAppMessage: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		if (res.from === 'button') {
		}
		var path=the.data.route+'?'+app.access.query;
		if(app.access.op){
			path +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			path +='&fromuser='+pagedata.fromuser;
		}
		return {
		  title: the.data.pagetitle,
		  path: path,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
	onShareTimeline: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		var query=app.access.query
		if(app.access.op){
			query +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			query +='&fromuser='+pagedata.fromuser;
		}
		query +='&isgoshare=1'
		return {
		  title: the.data.pagetitle,
		  query: query,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
});